<?php defined('BASEPATH') OR exit('No direct script access allowed');

class banner extends Services{
	private $status = TRUE;
	
	function banner(){
		parent::__construct();
				
	}
	
	/**
	 * Agrega un banner
	 */
	protected function add(){
		$this->checkAccessToken();
		$response_validate	= $this->validate('add');
		
		if($this->status){
			$this->load->model('banner_model');
			$data	= $this->banner_model->add(self::$params);
			if(is_array($data))
				return $this->parseOutput($data);
			else
				throw new NotExistException('ban');
		}
		else
			throw new UserValidateException(implode(',',$response_validate));
	}
	
	/**
	 * Modifica un banner
	 */
	protected function update(){
		$this->checkAccessToken();
		$response_validate	= $this->validate('update');
		
		if($this->status){
			$this->load->model('banner_model');
			$data	= $this->banner_model->update(self::$params);
			if(is_array($data))
				return $this->parseOutput($data);
			else
				throw new NotExistException('Banner');
		}
		else
			throw new UserValidateException(implode(',',$response_validate));
	}
	
	/**
	 * Elimina un banner
	 */
	protected function delete(){
		$this->checkAccessToken();
		$response_validate	= $this->validate('delete');
		
		if($this->status){
			$this->load->model('banner_model');
			$data	= $this->banner_model->delete(self::$params);
			if(is_array($data))
				return $this->parseOutput($data);
			else
				throw new NotExistException('Banner');
		}
		else
			throw new UserValidateException(implode(',',$response_validate));
	}
	
	/**
	 * Obtener los banners
	 */
	protected function get(){
		$this->checkConsumerKey();
		$response_validate	= $this->validate('get');
		
		if($this->status){
			$this->load->model('banner_model');
			$data	= $this->banner_model->get(self::$params);
			
			return $this->parseOutput($data);
		}
		else
			throw new UserValidateException(implode(',',$response_validate));
	}	
	

	private function validate($val_method){
    	if(method_exists($this, 'validate_'.$val_method)){
    		$this->load->library('MY_Form_validation');
    		return $this->{'validate_'.$val_method}();
    	}
    }
	
	private function validate_add(){
		$validate	= new MY_Form_validation();
		$validate->setArrayValidate(self::$params);
		
		$config	= array(
					array(
						'field' => 'url',
						'label'	=> 'url',
						'rules'	=> 'max_length[145]'
					),
					array(
						'field' => 'description',
						'label'	=> 'description',
						'rules'	=> 'max_length[2000]'
					),
					array(
						'field' => 'status',
						'label'	=> 'status',
						'rules'	=> 'expression:/^(0|1)$/'
					)
				);
		
		$validate->set_rules($config);
		$this->status = $validate->run();
		
		if(!isset($_FILES['img']['name'])){
			$this->status = FALSE;
			$validate->_error_array['img'] = lang('txt_invalid_field', 'img');
		}else if($_FILES['img']['size'] < 1){
			$this->status = FALSE;
			$validate->_error_array['img'] = lang('txt_invalid_field', 'img');
		}
		
		return $validate->_error_array;
	}
	
	private function validate_update(){
		$validate	= new MY_Form_validation();
		$validate->setArrayValidate(self::$params);
		
		$config	= array(
					array(
						'field' => 'banner',
						'label'	=> 'banner',
						'rules'	=> 'required|is_natural_no_zero'
					),
					array(
						'field' => 'url',
						'label'	=> 'url',
						'rules'	=> 'max_length[145]'
					),
					array(
						'field' => 'description',
						'label'	=> 'description',
						'rules'	=> 'max_length[2000]'
					),
					array(
						'field' => 'status',
						'label'	=> 'status',
						'rules'	=> 'expression:/^(0|1)$/'
					)
				);
		
		$validate->set_rules($config);
		$this->status = $validate->run();
		
		return $validate->_error_array;
	}
	
	private function validate_delete(){
		$validate	= new MY_Form_validation();
		$validate->setArrayValidate(self::$params);
		
		$config	= array(
					array(
						'field' => 'banner',
						'label'	=> 'banner',
						'rules'	=> 'required|is_natural_no_zero'
					)
				);
		
		$validate->set_rules($config);
		$this->status = $validate->run();
		
		return $validate->_error_array;
	}
	
	private function validate_get(){
		
		$validate	= new MY_Form_validation();
		$validate->setArrayValidate(self::$params);
		
		$config	= array(
						array(
								'field' => 'type',
								'label'	=> 'type',
								'rules'	=> 'expression:/^(all|active|deactive)$/'
						)
					);
		
		$validate->set_rules($config);
		$this->status = $validate->run();
		
		return $validate->_error_array;
	}
	
	
	
	private function parseOutput($data=array(), $only_parse=false){
		if ($only_parse)
			return $this->{'_format_'.$this->getFormat()}($data);
		if($this->status==true){
			return $this->{'_trim_format_'.$this->getFormat()}(
					$this->{'_format_'.$this->getFormat()}($data)
					);
		}
    }
	
}